﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using app.Models;
using AmuTools;

namespace app.Api
{
    /*
     * 投融资对接
    */
    public class ApiAbutment : ApiBase
    {
        public ApiAbutment(Controller ctx) : base(ctx) { }

        #region 自定义请求参数
        // 
        string req_finance_member_id { get { return GetStringParam(new ReqStringParamOption { Name = "finance_member_id" }); } }
        string req_investment_member_id { get { return GetStringParam(new ReqStringParamOption { Name = "investment_member_id" }); } }
        string req_finance_id { get { return GetStringParam(new ReqStringParamOption { Name = "finance_id" }); } }
        string req_investment_id { get { return GetStringParam(new ReqStringParamOption { Name = "investment_id", Level = REQPARAMLEVEL.FREE }); } }

        #endregion

        public void Do(string action)
        {
            // 1最新消息，2正在约谈，3已达成，4已取消
            string status_format = "(finance_status = {0} {1} investment_status = {2})";
            string status_str = "";

            switch (action)
            {
                case "receive_list":
                    {
                        if (UserRole == ROLE_TYPES.Investor)
                        {
                            switch (req_status)
                            {
                                case 1: status_str = string.Format(status_format, (int)ABUTMENT_STATUS.INTERVIEW, "and", (int)ABUTMENT_STATUS.NONE); break;
                                case 2: status_str = string.Format(status_format, (int)ABUTMENT_STATUS.INTERVIEW, "and", (int)ABUTMENT_STATUS.INTERVIEW); break;
                                case 3: status_str = string.Format(status_format, (int)ABUTMENT_STATUS.AGREE, "and", (int)ABUTMENT_STATUS.AGREE); break;
                                case 4: status_str = string.Format(status_format, (int)ABUTMENT_STATUS.CANCEL, "or", (int)ABUTMENT_STATUS.CANCEL); break;
                            }
                            // 收到的投递：当前为投资者，发起者为融资者，会员id为自己
                            string condition_str = string.Format("investment_member_id = '{0}' and sponsor = '{1}' and {2}", UserCode, ROLE_TYPES.Financier, status_str);
                            ResponseJson(GetPageResult<Abutment>(condition_str, G.DB.GetPrimaryKey<Abutment>()));
                        }
                        else if (UserRole == ROLE_TYPES.Financier)
                        {
                            switch (req_status)
                            {
                                case 1: status_str = string.Format(status_format, (int)ABUTMENT_STATUS.NONE, "and", (int)ABUTMENT_STATUS.INTERVIEW); break;
                                case 2: status_str = string.Format(status_format, (int)ABUTMENT_STATUS.INTERVIEW, "and", (int)ABUTMENT_STATUS.INTERVIEW); break;
                                case 3: status_str = string.Format(status_format, (int)ABUTMENT_STATUS.AGREE, "and", (int)ABUTMENT_STATUS.AGREE); break;
                                case 4: status_str = string.Format(status_format, (int)ABUTMENT_STATUS.CANCEL, "or", (int)ABUTMENT_STATUS.CANCEL); break;
                            }
                            // 收到的约谈：当前为融资者，发起者为投资者，会员id为自己
                            string condition_str = string.Format("finance_member_id = '{0}' and sponsor = '{1}' and {2}", UserCode, ROLE_TYPES.Investor, status_str);
                            ResponseJson(GetPageResult<Abutment>(condition_str, G.DB.GetPrimaryKey<Abutment>()));
                        }
                        else
                        {
                            ThrowStatus(STATUS.NO_RESOURCES, "没有符合当前身份角色的数据！");
                        }
                        break;
                    }
                case "deliver_list":
                    {
                        if (UserRole == ROLE_TYPES.Investor)
                        {
                            switch (req_status)
                            {
                                case 1: status_str = string.Format(status_format, (int)ABUTMENT_STATUS.NONE, "and", (int)ABUTMENT_STATUS.INTERVIEW); break;
                                case 2: status_str = string.Format(status_format, (int)ABUTMENT_STATUS.INTERVIEW, "and", (int)ABUTMENT_STATUS.INTERVIEW); break;
                                case 3: status_str = string.Format(status_format, (int)ABUTMENT_STATUS.AGREE, "and", (int)ABUTMENT_STATUS.AGREE); break;
                                case 4: status_str = string.Format(status_format, (int)ABUTMENT_STATUS.CANCEL, "or", (int)ABUTMENT_STATUS.CANCEL); break;
                            }
                            // 发起的约谈：当前为投资者，发起者为投资者，会员id为自己
                            string condition_str = string.Format("investment_member_id = '{0}' and sponsor = '{1}' and {2}", UserCode, ROLE_TYPES.Investor, status_str);
                            ResponseJson(GetPageResult<Abutment>(condition_str, G.DB.GetPrimaryKey<Abutment>()));
                        }
                        else if (UserRole == ROLE_TYPES.Financier)
                        {
                            switch (req_status)
                            {
                                case 1: status_str = string.Format(status_format, (int)ABUTMENT_STATUS.INTERVIEW, "and", (int)ABUTMENT_STATUS.NONE); break;
                                case 2: status_str = string.Format(status_format, (int)ABUTMENT_STATUS.INTERVIEW, "and", (int)ABUTMENT_STATUS.INTERVIEW); break;
                                case 3: status_str = string.Format(status_format, (int)ABUTMENT_STATUS.AGREE, "and", (int)ABUTMENT_STATUS.AGREE); break;
                                case 4: status_str = string.Format(status_format, (int)ABUTMENT_STATUS.CANCEL, "or", (int)ABUTMENT_STATUS.CANCEL); break;
                            }
                            // 收到的约谈：当前为融资者，发起者为融资者，会员id为自己
                            string condition_str = string.Format("finance_member_id = '{0}' and sponsor = '{1}' and {2}", UserCode, ROLE_TYPES.Financier, status_str);
                            ResponseJson(GetPageResult<Abutment>(condition_str, G.DB.GetPrimaryKey<Abutment>()));
                        }
                        else
                        {
                            ThrowStatus(STATUS.NO_RESOURCES, "没有符合当前身份角色的数据！");
                        }
                        break;
                    }
                case "begin":
                    {
                        Abutment abutment = new Abutment { finance_id = req_finance_id, investment_id = req_investment_id };
                        if (UserRole == ROLE_TYPES.Investor)
                        {
                            abutment.investment_member_id = UserCode;
                            abutment.finance_member_id = req_finance_member_id;
                            abutment.investment_status = (int)ABUTMENT_STATUS.INTERVIEW;
                        }
                        else if (UserRole == ROLE_TYPES.Financier)
                        {
                            abutment.investment_member_id = req_investment_member_id;
                            abutment.finance_member_id = UserCode;
                            abutment.finance_status = (int)ABUTMENT_STATUS.INTERVIEW;
                        }
                        G.DB.Insert<Abutment>(abutment);
                        ResponseJson(new Result());
                        break;
                    }
                case "agree":
                    {
                        Abutment abutment = G.DB.GetById<Abutment>(req_id);
                        if (UserRole == ROLE_TYPES.Investor)
                        {
                            switch ((ABUTMENT_STATUS)abutment.finance_status)
                            {
                                case ABUTMENT_STATUS.NONE: ;break;
                                case ABUTMENT_STATUS.INTERVIEW: abutment.investment_status = (int)ABUTMENT_STATUS.INTERVIEW; break;
                                case ABUTMENT_STATUS.AGREE: abutment.investment_status = (int)ABUTMENT_STATUS.AGREE; break;
                                case ABUTMENT_STATUS.CANCEL:; break;
                            }
                        }
                        else if (UserRole == ROLE_TYPES.Financier)
                        {
                            switch ((ABUTMENT_STATUS)abutment.investment_status)
                            {
                                case ABUTMENT_STATUS.NONE:; break;
                                case ABUTMENT_STATUS.INTERVIEW: abutment.finance_status = (int)ABUTMENT_STATUS.INTERVIEW; break;
                                case ABUTMENT_STATUS.AGREE: abutment.finance_status = (int)ABUTMENT_STATUS.AGREE; break;
                                case ABUTMENT_STATUS.CANCEL:; break;
                            }
                        }
                        G.DB.Update<Abutment>(abutment);
                        ResponseJson(new Result());
                        break;
                    }
                case "cancel":
                    {
                        Abutment abutment = G.DB.GetById<Abutment>(req_id);
                        if (UserRole == ROLE_TYPES.Investor)
                        {
                            abutment.investment_status = (int)ABUTMENT_STATUS.CANCEL;
                        }
                        else if (UserRole == ROLE_TYPES.Financier)
                        {
                            abutment.finance_status = (int)ABUTMENT_STATUS.CANCEL;
                        }
                        G.DB.Update<Abutment>(abutment);
                        ResponseJson(new Result());
                        break;
                    }
                default: break;
            }
        }
    }


}